<template>
	<view>
		<uni-list>
			<uni-list-item title="启用秒杀" :show-switch="true" :switchChecked="seckillUsing"
				@switchChange="switchSeckill"></uni-list-item>
		</uni-list>
		<view class="gaps">

		</view>

		<view class="goods-panel" v-if="goods.length">
			<view v-for="item in goods" :key="item" @click="limitClick(item)" class="goods-item">
				<view slot="header">
					<image class="slot-image" :src="item.logo || '/static/logo.png'" mode="scaleToFill"
						@click="changeLimit(item)"></image>
				</view>
				<view slot="body">
					<view class="slot-body">
						<view>{{item.name}}</view>
						<view>{{item.intro}}</view>
						<view>原价:{{priceTransform(item.topPrice)}}/{{item.unit}}</view>
						<view>秒杀价:{{priceTransform(item.price)}}/{{item.unit}}</view>
						<view>库存:{{item.stock}}</view>
						<view>开始时间:{{formatDateTime(item.startTime)}} </view>
						<view>结束时间:{{formatDateTime(item.endTime)}} </view>
						<view>状态:{{goodsStatus(item.status)}} </view>
					</view>
				</view>
			</view>
		</view>
		
		<u-empty v-else mode="list" >
		</u-empty>


		<view class="gaps">

		</view>
		<u-button type="success" @click="addGoodsClick" text="添加商品"></u-button>


		<uni-popup ref="limitPop" type="center" :is-mask-click="false">
			<view class="limit-form">
				<view class="goods-name">{{tmpName}}</view>
				<uni-forms ref="baseForm" :modelValue="limitForm">
					<uni-forms-item label="秒杀价(分)" required>
						<uni-easyinput type="number" v-model="limitForm.price" placeholder="请输入秒杀价" />
					</uni-forms-item>
					<uni-forms-item label="划线价(分)" required>
						<uni-easyinput type="number" v-model="limitForm.topPrice" placeholder="请输入划线价" />
					</uni-forms-item>
					<uni-forms-item label="库存" required>
						<uni-easyinput type="number" v-model="limitForm.stock" placeholder="请输入库存" />
					</uni-forms-item>
				</uni-forms>
				<view class="form-btn">
					<view>
						<u-button type="default" @click="cancelPop" text="取消"></u-button>
					</view>
					<view>
						<u-button type="success" @click="confirmLimit" text="确定"></u-button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getShopGoods,
		delSeckillGoods,
		editSeckillGoods,
		beginSeckill
	} from '@/api/shops/seckillManage/seckillManage.js'
	import {
		enableSeckill,
		getMarketStatus,
	} from '@/api/shops/lotteryManage/lotteryManage.js'
	import {
		transform
	} from '@/minix/mixin.js'
	export default {
		mixins: [transform],
		data() {
			return {
				goods: [],
				selGoods:{},
				tmpName:'',
				limitForm:{},
				shopId:'',
				seckillUsing:false
			}
		},
		onLoad(options) {
			this.shopId = options.shopId
			this.init()
		},
		methods: {
			init() {
				getMarketStatus().then((res) => {
					this.seckillUsing = res.seckillStatus === 1
				})
				this.getAllGoods()
				
			},
			goodsStatus(status) {
				if(status == 0) {
					return '待开启'
				}
				if(status == 1) {
					return '已开启'
				}
				if(status == 2) {
					return '活动中'
				}
				if(status = 3) {
					return '已结束'
				}
			},
			switchSeckill(e) {
				enableSeckill(e.value ? 1 : 0).then((res) => {
					this.seckillUsing = e.value
					uni.showToast({
						title: '切换成功',
						icon: 'none'
					})
				})
			},
			getAllGoods() {
				getShopGoods().then((res) => {
					this.goods = res
				})
			},
			addGoodsClick() {
				let _this = this;
				uni.navigateTo({
					url: '/subpkg/shops/addSeckillGoods/addSeckillGoods?shopId=' + _this.shopId, // 1表示限购
					events: {
						addSeckillGoodsEvent: function(data) {
							if (data.data) {
								_this.init()
							}
						}
					}
				})
			},
			limitClick(item) {
				let _this = this
				this.selGoods = item;
				uni.showActionSheet({
					itemList:['开启','编辑','删除'],
					success: function(res) {
						if(res.tapIndex == 1) {
							_this.tmpName = item.name;
							_this.limitForm = {
								id: item.id,
								price: item.price,
								topPrice: item.topPrice,
								stock: item.stock,
							}
							_this.$refs.limitPop.open()
						} else if(res.tapIndex == 2) {
							uni.showModal({
								title:'确定删除吗',
								success: (res) => {
									if(res.confirm) {
										delSeckillGoods(item.id).then((r) => {
											_this.goods = _this.goods.filter((e) => {return e.id != item.id})
										})
									}
								}
							})
						} else if(res.tapIndex == 0) {
							uni.showModal({
								title:'确定开启?',
								success: (r) => {
									if(r.confirm) {
										beginSeckill(item.id).then((result) => {
											item.status = 1
											uni.showToast({
												icon:'none',
												title:'开启成功'
											})
										}) 
									}
								}
							})
						}
					}
				})
			},
			cancelPop() {
				this.$refs.limitPop.close()
			},
			confirmLimit() {
				let _this = this
				uni.showModal({
					title: '确定修改吗',
					success: (res) => {
						if (res.confirm) {
							editSeckillGoods(_this.limitForm).then((r) => {
								let g = _this.goods.filter(e => {
									return e.id == _this.limitForm.id
								})[0]

								g.price = _this.limitForm.price
								g.topPrice = _this.limitForm.topPrice
								g.stock = _this.limitForm.stock

								this.$refs.limitPop.close()

								uni.showToast({
									title: '修改成功',
									icon: 'none'
								})

							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-item{
		display: flex;
		flex-direction: row;
		border-bottom: 1px solid #555555;
		margin-bottom: 5px;
		padding: 10px;
	}
	.slot-image {
		width: 100px;
		height: 100px;
		border-radius: 8px;
	}

	.slot-body {
		margin-left: 10px;
		flex: 1;
	}

	.limit-form {
		background-color: #fff;
		padding: 10px;
		width: 90vw;
	}
	.goods-name{
		text-align: center;
		padding:10px;
		font-size: 16px;
	}

	.form-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
	}
</style>